<?php


namespace app\service;


use think\Db;

class cxyunServiceV2
{
    const URL = "http://api.cxzntc.com:9900/park/cxzn/interface/";//'http://ykt.test.cxyun.net.cn:7303/cxzn/interface/';
    //const Appkey = 'f9e926d34cb8aa092dbbaac87f4';//'b620f6be42328effc32b982a2b9';
    public $appkey;

    // 构造函数接受一个参数$value，并将其赋值给属性$property
    public function __construct($value) {
        $this->appkey = $value;
    }
    public function commonPost($api,$data,$notSignData){
        $url=self::URL.$api;
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $data=array_merge($data,$notSignData);
        $result=$this->https_post($url,$data);


        $log["api"]=$api;
        $log["res"]=$result;//$json = json_encode($data, JSON_UNESCAPED_UNICODE);
        $log["params"]=json_encode($data,JSON_UNESCAPED_UNICODE);
        $log["createTime"]=date('Y-m-d H:i:s');
        Db::table('cxyun_api_log')->insert($log);
        return $result;
    }
    //增加修改月租
    public function saveMonthlyRent($data){
        $url=self::URL."saveMonthlyRent";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $result=$this->https_post($url,$data);
        return $result;
    }

    //用于根据车场ID获取所以月卡车
    public function test(){
        $url=self::URL."getParkCarType";
        $data["parkingId"]="5343054078414340259";

        $data["timestamp"]="1703470234000";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));

        //$result=$this->https_post($url,$data);
        //echo "<pre>";
        var_dump($data);
    }
    //用于根据车场ID获取所以月卡车
    public function getMonthlyRentList($data){
        $url=self::URL."getMonthlyRentList";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $result=$this->https_post($url,$data);
        return $result;
    }

    //获取月租车信息
    public function getMonthlyRent($data){
        $url=self::URL."getMonthlyRent";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $result=$this->https_post($url,$data);
        return $result;
    }

    //月卡续期
    public function payMonthly($data){
        $url=self::URL."payMonthly";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $result=$this->https_post($url,$data);
        return $result;
    }


    //获取车场车型信息
    function getParkCarModel($parkingId){
        $url=self::URL."getParkCarModel";
        $data["parkingId"]=$parkingId;
        $data["timestamp"]=time()."000";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        $result=$this->https_post($url,$data);
        return $result;
        //echo "<pre>";
        var_dump($result);
    }

    //获取车场车类信息
    function getParkCarType($parkingId){
        $url=self::URL."getParkCarType";
        $data["parkingId"]=$parkingId;

        $data["timestamp"]=time()."000";
        $data["sign"]=$this->createSign($data);
        $data["appKey"]=strtoupper(md5($this->appkey));
        //$data["appKey"]=self::Appkey;
        //echo "<pre>";
        //var_dump($data);
        $result=$this->https_post($url,$data);
        return $result;
        //echo "<pre>";
        // var_dump($result);

    }

    //生成签名
    private function createSign($arr){
        //签名步骤一：按字典序排序参数
        ksort($arr);
        $string = $this->ToUrlParams($arr);
        //echo $string;echo"<br/>";
        //echo md5(self::Appkey);echo"<br/>";
        //签名步骤二：在string后加入KEY
        $string = (string)$string . strtoupper(md5($this->appkey));
        //签名步骤三：MD5加密
        //echo $string."<br/>";
        $string = md5($string);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        //echo $result."<br/>";
        return $result;
    }



    private function ToUrlParams($value){
        $buff = "";
        foreach($value as $k => $v){
            //echo $k;echo "<br/>";

            //echo $buff ;echo "<br/>";
            // if ( $k == 'timestamp'){
            //     $buff .= 'amp;timestamp' . '=' . $v. "&";
            // }else{
            //     $buff .= $k . "=" . $v . "&";
            // }
            $buff .= $k . "=" . $v . "&";
//            if($k != "sign" && $v != "" && !is_array($v)){
//                $buff .= $k . "=" . $v . "&";
//            }
        }
        //echo $buff ;echo"<br/>";
        $buff = trim($buff, "&");
        return $buff;
    }
    public function https_post($url,$data)
    {
        $jsonData = json_encode($data,JSON_UNESCAPED_UNICODE);
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($jsonData))
        );
        $result = curl_exec($curl);
        if (curl_errno($curl)) {
            return 'Errno'.curl_error($curl);
        }
        curl_close($curl);
        return $result;
    }
    public function post_curl_data($url,$xml){
        //CURL模拟请求
        $curls = curl_init(); //启动一个CURL会话
        curl_setopt($curls, CURLOPT_URL, $url); //要访问的地址
        curl_setopt($curls, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curls, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curls, CURLOPT_HEADER, FALSE);//设置header
        curl_setopt($curls, CURLOPT_RETURNTRANSFER, TRUE);//要求结果为字符串且输出到屏幕上
        curl_setopt($curls, CURLOPT_POST, TRUE); //发送一个常规的Post请求
        curl_setopt($curls, CURLOPT_POSTFIELDS, $xml); //Post提交的数据包
        curl_setopt($curls, CURLOPT_TIMEOUT, 30); //设置超时限制防止死循环
        $result = curl_exec($curls); //执行操作
        //$err = curl_error($curls);//输出错误
        curl_close($curls); //关闭CURL会话
        return $result;
    }
}